<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\News;

class NewsService extends BaseService
{

    public function list(){
        $model = News::query()->with('newsCategory')->select('id','title','cover','category_id','content','author','created_at')->where('status', 1)->where('deleted', 0)->orderBy('id', 'DESC');
        $list = $this->usePage($model)->toArray();

        foreach($list['data'] as $key=>$val)
        {
            $list['data'][$key]['content'] = mb_substr(strip_tags($val['content']), 0, 100);
        }

        return $list;
    }

    public function detail(array $params)
    {
        $news = News::query()->with('newsCategory')->select('id','title','cover','category_id','content','author','created_at')->where('id', $params['id'])->where('status', 1)->where('deleted', 0)->first();
        if(!$news)
        {
            throw new ApiException(ErrorCode::WARNING, '内容不存在');
        }

        return $news;
    }

}